package com.me.mysqladmin.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

/**
 * 将各种类型格式化为EasyUI控件所需的list或Map
 */
public class EasyUIFormat {
	// 日志记录器
	private final static Logger LOG = LoggerFactory.getLogger(EasyUIFormat.class);
	
	// 表格的列宽
	private final static Integer COLWIDTH = 100;
	// 表格的最大显示行数（若显示过多，浏览器会崩掉）
	private final static Integer MAX_ROWS = 500;
	
	/**
	 * 格式化表格数据（单列）
	 */
	public static <T> Map<String, Object> parseGrid(String fieldName, List<T> list){
		Assert.notNull(fieldName);
		Assert.notNull(list);
		Map<String, Object> grid = new LinkedHashMap<>();
		// 总记录数
		grid.put("total", list.size());
		// 行记录
		List<Map<String, Object>> rows = new ArrayList<>();
		for(Object obj : list){
			Map<String, Object> rowMap = new LinkedHashMap<>();
			rowMap.put(fieldName, obj);
			rows.add(rowMap);
		}
		grid.put("rows", rows);
		// 列字段
		List<Map<String, Object>> columns = new ArrayList<>();
		Map<String, Object> column = new LinkedHashMap<>();
		column.put("field", fieldName);
		column.put("title", fieldName);
		column.put("width", COLWIDTH);
		columns.add(column);
		grid.put("columns", columns);
		return grid;
	}
	
	/**
	 * 格式化表格数据（多列）
	 */
	public static <T> Map<String, Object> parseGrid(List<?> list, Class<T> elementType) {
		Assert.notNull(list);
		ListUtil.subList(list, MAX_ROWS);
		LOG.info("--------总记录数：" + list.size() + "--------");
		// 封装返回值
		Map<String, Object> grid = new LinkedHashMap<>();
		// 总记录数
		grid.put("total", list.size());
		// 行记录
		List<Map<String, Object>> rows = new ArrayList<>();
		for(Object obj : list){
			Map<String, Object> rowMap = BeanUtil.toMap(obj);
			// 如果是空行，则将总记录数-1
			boolean flag = true;
			for(String key : rowMap.keySet()){
				if(!NullUtil.isNull(rowMap.get(key))){
					flag = false;
					if(rowMap.get(key) instanceof Timestamp){
						Timestamp value = (Timestamp) rowMap.get(key);
						rowMap.put(key, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(value.getTime())));
					}
				}
			}
			if(flag){
				int total = (int) grid.get("total");
				grid.put("total", total - 1);
			} else{
				rows.add(rowMap);
			}
		}
		grid.put("rows", rows);
		// 列字段
		List<Map<String, Object>> columns = new ArrayList<>();
		List<String> colNames = new ArrayList<>();	// 列名集合
		colNames = BeanUtil.getProps(ListUtil.getElement(list, elementType));
		for(String colName : colNames){
			Map<String, Object> column = new LinkedHashMap<>();
			column.put("field", colName);
			column.put("title", colName);
			column.put("width", COLWIDTH);
			columns.add(column);
		}
		grid.put("columns", columns);
		return grid;
	}
	
	/**
	 * 格式化二级树
	 */
	public static List<Map<String, Object>> parseTree(Map<String, List<String>> map){
		List<Map<String, Object>> trees = new ArrayList<>();
		Set<String> keySet = map.keySet();
		// 一级菜单
		for(String levelOne : keySet){
			Map<String, Object> tree = new LinkedHashMap<>();
			tree.put("id", levelOne);
			tree.put("text", levelOne);
			tree.put("iconCls", "icon-db");
			// 去除子节点中的空值
			List<String> childNodes = map.get(levelOne);
			ListUtil.removeNullVal(childNodes);
			if(childNodes.size() > 0){
				tree.put("state", "closed");
			}
			// 二级菜单
			List<Map<String, Object>> subTrees = new ArrayList<>();
			for(String levelTwo : map.get(levelOne)){
				if(levelTwo == null || "".equals(levelTwo)){
					continue;
				}
				Map<String, Object> subTree = new LinkedHashMap<>();
				subTree.put("id", levelTwo);
				subTree.put("text", levelTwo);
				subTree.put("iconCls", "icon-table");
				subTrees.add(subTree);
			}
			tree.put("children", subTrees);
			trees.add(tree);
		}
		return trees;
	}
}
